/*                                                                         
 * Copyright 2010-2013 the original author or authors.                     
 *                                                                         
 * Licensed under the Apache License, Version 2.0 (the "License");         
 * you may not use this file except in compliance with the License.        
 * You may obtain a copy of the License at                                 
 *                                                                         
 *      http://www.apache.org/licenses/LICENSE-2.0                         
 *                                                                         
 * Unless required by applicable law or agreed to in writing, software     
 * distributed under the License is distributed on an "AS IS" BASIS,       
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and     
 * limitations under the License.                                          
 */
package com.shopng.geode.common;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.shopng.utils.LogUtil;

/**
 * @author tkong
 *         
 */
public final class JsonUtils
{
  private final static ObjectMapper mapper;
  
  static
  {
    mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
  }
  
  public final static <T> T jsonToObject(byte[] bytes, Class<T> classType)
  {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    T value = null;
    try
    {
      value = mapper.readValue(bytes, classType);
    }
    catch (IOException e)
    {
      LogUtil.getCoreLog().warn("Json bytes convert to class {} failed", e, classType.getName());
    }
    return value;
  }
  
  public final static <T> T jsonToObject(String jsonString, Class<T> classType)
  {
    
    T value = null;
    try
    {
      if(!StringUtils.isEmpty(jsonString))
        value = mapper.readValue(jsonString, classType);
    }
    catch (IOException e)
    {
      LogUtil.getCoreLog().warn("Json string {} convert to class {} failed", e, jsonString, classType.getName());
    }
    return value;
  }
  
  public final static String objectToJson(Object value)
  {
    // If value is String, do not convert, return
    if (value instanceof String)
      return (String) value;
    String str = null;
    try
    {
      str = mapper.writeValueAsString(value);
    }
    catch (IOException e)
    {
      LogUtil.getCoreLog().warn("Object convert to string failed", e);
    }
    return str;
  }
  
  public final static byte[] objectToBytes(Object value)
  {
    byte[] dataBytes = null;
    try
    {
      dataBytes = mapper.writeValueAsBytes(value);
    }
    catch (IOException e)
    {
      LogUtil.getCoreLog().warn("Object convert to byte[] failed", e);
    }
    return dataBytes;
  }
  
  public final static <T> T convertValue(Object value, Class<T> toClass)
  {
    try
    {
      T resutl = null;
      if (value != null)
        resutl = mapper.convertValue(value, toClass);
      return resutl;
    }
    catch (Exception e)
    {
      LogUtil.getCoreLog().warn("Object convert to {} failed", toClass.getSimpleName(), e);
    }
    return null;
  }
  
  public static void main(String[] args)
  {
    String address = "192.168.120.82[12244]";
    byte[] aaa = JsonUtils.objectToBytes(address);
    System.out.println(JsonUtils.jsonToObject(aaa, String.class));
  }
}
